
#include "gather_v3_tiling.h"
#include "register/op_def_registry.h"
using namespace std;

namespace optiling {
static ge::graphStatus TilingFunc(gert::TilingContext* context)
{

  GatherV3TilingData tiling;
  
  auto shape_x = context -> GetInputTensor(0) -> GetOriginShape();
  auto shape_indices = context -> GetInputTensor(1) -> GetOriginShape();
  auto shape_axis = context -> GetInputTensor(2) -> GetOriginShape();
  auto batchdim = *context -> GetAttrs() -> GetInt(0);
  uint64_t xdim = shape_x.GetDimNum();
  uint64_t indicesdim = shape_indices.GetDimNum();

  uint64_t x_ndarray[20];
  uint64_t indices_ndarray[20];

  for(int i=0;i<xdim;i++){
        x_ndarray[i]=shape_x.GetDim(i);
  }

  for(int i=0;i<indicesdim;i++){
        indices_ndarray[i]=shape_indices.GetDim(i);
  }


  tiling.set_x_ndarray(x_ndarray);
  tiling.set_batchdim(batchdim);
  tiling.set_xdim(xdim);
  tiling.set_indicesdim(indicesdim);
  tiling.set_indices_ndarray(indices_ndarray);
  context->SetBlockDim(1);
  tiling.SaveToBuffer(context->GetRawTilingData()->GetData(), context->GetRawTilingData()->GetCapacity());
  context->GetRawTilingData()->SetDataSize(tiling.GetDataSize());

  return ge::GRAPH_SUCCESS;
}
}

namespace ge {
static ge::graphStatus InferShape(gert::InferShapeContext* context)
{
    const gert::Shape* x1_shape = context->GetInputShape(0);
    gert::Shape* y_shape = context->GetOutputShape(0);
    *y_shape = *x1_shape;
    return GRAPH_SUCCESS;
}
static ge::graphStatus InferDataType(gert::InferDataTypeContext *context)
{
const auto inputDataType = context->GetInputDataType(0);
context->SetOutputDataType(0, inputDataType);
return ge::GRAPH_SUCCESS;
}
}


namespace ops {
class GatherV3 : public OpDef {
public:
    explicit GatherV3(const char* name) : OpDef(name)
    {
        this->Input("x")
            .ParamType(REQUIRED)
            .DataType({ge::DT_INT8, ge::DT_FLOAT16, ge::DT_INT32, ge::DT_FLOAT})
            .Format({ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND})
            .UnknownShapeFormat({ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND});
        this->Input("indices")
            .ParamType(REQUIRED)
            .DataType({ge::DT_INT32, ge::DT_INT32, ge::DT_INT32, ge::DT_INT32})
            .Format({ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND})
            .UnknownShapeFormat({ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND});
        this->Input("axis")
            .ParamType(REQUIRED)
            .DataType({ge::DT_INT32, ge::DT_INT32, ge::DT_INT32, ge::DT_INT32})
            .Format({ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND})
            .UnknownShapeFormat({ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND});
        this->Output("out")
            .ParamType(REQUIRED)
            .DataType({ge::DT_INT8, ge::DT_FLOAT16, ge::DT_INT32, ge::DT_FLOAT})
            .Format({ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND})
            .UnknownShapeFormat({ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND});
        this->Attr("batch_dims").AttrType(OPTIONAL).Int(0);
        this->Attr("negative_index_support").AttrType(OPTIONAL).Bool(false);

        this->SetInferShape(ge::InferShape).SetInferDataType(ge::InferDataType);

        this->AICore()
            .SetTiling(optiling::TilingFunc);
        this->AICore().AddConfig("ascend910b");
        this->AICore().AddConfig("ascend310b");
    }
};

OP_ADD(GatherV3);
}
